﻿using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls.Primitives;

using Silversocial.Client.Modules;

using Silverstone;
using Silverstone.DI;

namespace Silversocial.Client.Views
{
    public partial class App : Application
    {
        private readonly IContainer container;

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            this.container = new Container();

            InitializeComponent();
        }

        private void InitializeContainer()
        {
            CommandManager.Initialise();

            this.container.RegisterForCreation<Shell, IAppShell>();

            this.container.RegisterForCreation<LoginViewModel, LoginViewModel>();
            this.container.RegisterForCreation<LoginPage, ILoginView>();
            this.container.RegisterForCreation<DummyLoginDataProvider, ILoginDataProvider>();

            this.container.RegisterForCreation<Dummy, IFriendListView>();
            this.container.RegisterForCreation<Dummy, IRegisterView>();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            InitializeContainer();
            
            IAppShell shell = this.container.Get<IAppShell>();
            shell.Navigate<ILoginView>();
            this.RootVisual = shell as UIElement;
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            HtmlPage.Window.Alert(e.ExceptionObject.ToString());
        }

        public class Dummy : IFriendListView, IRegisterView
        {
            public void OnLoad()
            {
                throw new NotImplementedException();
            }

            public void OnUnload()
            {
                throw new NotImplementedException();
            }
        }
    }
}
